package com.boil.qz.safekavass.web.controller;

import com.boil.pojo.Page2Vo;
import com.boil.pojo.ResultVo;
import com.boil.qz.safekavass.model.Question;
import com.boil.qz.safekavass.pojo.QuestionVo;
import com.boil.qz.safekavass.service.QuestionService;
import com.boil.qz.safekavass.web.request.param.IdParam;
import com.boil.qz.safekavass.web.request.param.QuestionListParam;
import com.boil.util.Constants;
import com.boil.util.OkBaseController;
import com.boil.util.Page2;
import com.boil.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

/**
 * Created by ChenYong on 2017-04-12.
 * <br>
 * 题库 Controller。
 */
@Controller
@RequestMapping("/web/question")
public class QuestionController extends OkBaseController {
    /**
     * 注入题库 Service
     */
    @Autowired
    private QuestionService questionService;

    /**
     * 题目列表页面。
     *
     * @param model 模型
     * @return 题目列表页面
     */
    @RequestMapping(method = RequestMethod.GET, value = "/list.html")
    public String listHtml(Model model) {
        return "/question/list";
    }

    /**
     * 上传题库 Excel。
     *
     * @param model       模型
     * @param uploadExcel 上传的题库 Excel 文件
     * @return 结果 json
     * @throws Exception 抛出异常
     */
    @RequestMapping(method = RequestMethod.POST, value = "/upload.json")
    @ResponseBody
    public ResultVo uploadExcel(Model model, MultipartFile uploadExcel) throws Exception {
        // 结果 Vo
        ResultVo resultVo = new ResultVo();

        questionService.doQuestion(resultVo, getLoginUser(), uploadExcel);

        return resultVo;
    }

    /**
     * 获取题目列表 json。
     *
     * @param model  模型
     * @param param  题目列表的请求参数
     * @param errors 请求参数的校验结果
     * @return 题目列表 json
     */
    @RequestMapping(method = RequestMethod.GET, value = "/list.json")
    @ResponseBody
    public ResultVo listJson(Model model, @Valid QuestionListParam param, BindingResult errors) {
        // 结果 Vo
        ResultVo resultVo = new ResultVo();

        // 判断请求参数是否校验通过
        Utils.hasRequestParamError(resultVo, errors);

        // 查询分页实例
        Page2<QuestionVo> page2 = Utils.getPage2(request, param, Constants.ID_STR);
        page2.getParams().put("industryCode", param.getIndustryCode());
        page2.getParams().put("questionTitle", param.getQuestionTitle());
        page2.getParams().put("questionType", param.getQuestionType());
        page2.getParams().put("questionAnswer", param.getQuestionAnswer());
        page2.getParams().put("startDate", param.getStartDate());
        page2.getParams().put("endDate", param.getEndDate());

        // 分页查询
        questionService.findInPage(page2);

        // 获取 DataTables 专用分页映射实例
        Page2Vo<QuestionVo> page2Vo = Utils.getPage2Vo(param, page2);

        resultVo.setResult(page2Vo);

        return resultVo;
    }

    /**
     * 获取题目详细页面。
     *
     * @param model  模型
     * @param param  ID 参数
     * @param errors 请求参数的校验结果
     * @return 题目详细页面
     */
    @RequestMapping(method = RequestMethod.GET, value = "/detail.html")
    public String detailHtml(Model model, @Valid IdParam param, BindingResult errors) {
        // 结果 Vo
        ResultVo resultVo = new ResultVo();

        Question question = questionService.findOne(param.getId());

        resultVo.setResult(question);

        model.addAttribute(Constants.RESULT_VO, resultVo);

        return "/question/detail";
    }
}